﻿namespace DataMorph.Web.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Web.Http;

    using DataMorph.Common.Contracts;
    using DataMorph.Common.Entities;
    using DataMorph.Web.Models;
    using DataMorph.Web.Services;

    public class DataGeneratorController : ApiController
    {
        private readonly IDataMorphService dataMorphService;

        public DataGeneratorController(IDataMorphService dataMorphService)
        {
            this.dataMorphService = dataMorphService;
        }

        // PUT api/datagenerator/generatedata
        [HttpPut]
        public string PutGenerateData(GenerationParametersModel parametersModel)
        {
            if (this.ModelState.IsValid)
            {
                parametersModel.TimeRequested = DateTime.UtcNow;
                return this.dataMorphService.GenerateData(
                                                    parametersModel.ClientId,
                                                    parametersModel.StorageAccountName,
                                                    parametersModel.ToGenerationParameters());
            }

            return string.Empty;
        }

        // PUT api/datagenerator/canceldatageneration
        [HttpPut]
        public void PutCancelDataGeneration(string taskId)
        {           
            this.dataMorphService.CancelTask(taskId);
        }

        // GET api/datagenerator/getstorageaccounts
        [HttpGet]
        public IEnumerable<StorageAccountDetailsModel> GetStorageAccounts()
        {
            // make sure the storage accounts details table exists, and read, return all rows
            return this.dataMorphService.GetStorageAccounts();
        }

        // GET api/datagenerator/getdatasets
        [HttpGet]
        public IEnumerable<GenerationParametersModel> GetDataSets(string accountName)
        {
            // Read all of the containers given a stroage account name, you will need to read from the storage account tables first.
            return this.dataMorphService.GetDataSets(accountName);
        }

        // GET api/datagenerator/getprocessinginstances
        [HttpGet]
        public IEnumerable<ProcessingRecord> GetProcessingInstances(string storageAccount)
        {
            return this.dataMorphService.GetProcessingInstances(storageAccount);
        }

        // POST api/datagenerator/processdataset
        [HttpPost]
        public string ProcessDataSet(GenerationParametersModel parametersModel)
        {
            if (this.ModelState.IsValid)
            {
                return this.dataMorphService.ProcessDataSet(parametersModel.ClientId, parametersModel.ToGenerationParameters());
            }

            return null;
        }
    }
}